/**
 * 
 */
package com.itinsys.enma.manager.impl;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import com.itinsys.enma.dao.TraceDAO;
import com.itinsys.enma.dao.UserDAO;
import com.itinsys.enma.domain.DailyReservation;
import com.itinsys.enma.domain.PlanningReservation;
import com.itinsys.enma.domain.Reservation;
import com.itinsys.enma.domain.Trace;
import com.itinsys.enma.domain.User;
import com.itinsys.enma.exception.reservation.ReservationExistsException;
import com.itinsys.enma.manager.ReservationManager;
import com.itinsys.enma.manager.TraceManager;
import com.itinsys.enma.service.SingletonService;

/**
 * @author pferraz
 * 
 */
public class TraceManagerImpl implements TraceManager {

	@Autowired
	private TraceDAO traceDAO;
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private ReservationManager reservationManager;
	@Autowired
	private SingletonService singletonService;

	@Override
	public Boolean addTrace(Trace t) {

		return traceDAO.addTrace(t);
	}

	@Override
	public Trace reservationToTrace(Reservation r, String modifType)throws ReservationExistsException {
		String userName = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
		SimpleDateFormat dF=new SimpleDateFormat("dd/MM/yyyy");
		User user = userDAO.getByUserName(userName, true);
		String reservType = (r instanceof PlanningReservation) ? "planning": "daily";
		Trace t = null;
		if (modifType.equalsIgnoreCase("Creada")) {
			t = new Trace(reservType, modifType, new Timestamp(	new Date().getTime()), "", user, r);
		} else if (modifType.equalsIgnoreCase("Modificada")) {
			String previousData="";
			if (r instanceof PlanningReservation) {
				PlanningReservation pR=reservationManager.getReservationById(r.getId());
				previousData = pR.getDescription() + " " + pR.getsGroup() + " " + pR.getEnvironment().getDescription() + " " + singletonService.getDays().get(pR.getDay())
						+ " " + pR.getStartTime() + " " + pR.getEndTime() + " " 
						+ pR.getPlanning().getUser().getUsername();
			} else {
				DailyReservation dR = (DailyReservation) r;
				previousData = dR.getDescription() + " " + dR.getEnvironment().getDescription() + " " +dF.format(dR.getStartDate())
						+ " " + dR.getStartTime() + "  " +dF.format(dR.getEndDate())+ " " + dR.getEndTime() + " "+ dR.getUser().getUsername();
			}
			t = new Trace(reservType, modifType, new Timestamp(	new Date().getTime()), previousData, user, r);
		} else {
			String previousData="";
			if (r instanceof PlanningReservation) {
				PlanningReservation pR = (PlanningReservation) r;
				previousData = pR.getDescription() + " " + pR.getsGroup() + " " + pR.getEnvironment().getDescription() + " " +singletonService.getDays().get(pR.getDay())
						+ " " + pR.getStartTime() + " " + pR.getEndTime() + " "
						+ pR.getPlanning().getCareer() + " "
						+ pR.getPlanning().getCareerYear() + " "
						+ pR.getPlanning().getStatus() + " "
						+ pR.getPlanning().getUser().getUsername();
			} else {
				DailyReservation dR = (DailyReservation) r;
				previousData = dR.getDescription() + " " + dR.getEnvironment().getDescription() + " " +dF.format(dR.getStartDate())
						+ " " + dR.getStartTime() + "  " +dF.format(dR.getEndDate())+ " " + dR.getEndTime() + " "+ dR.getUser().getUsername();
			}

			t = new Trace(reservType, modifType, new Timestamp(	new Date().getTime()), previousData, user, null);
		}

		return t;
	}

}
